package com.ruoyi.web.controller.coin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.coin.service.ICoinBaseSqlService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.MjkjUtils;
import com.ruoyi.common.utils.blade.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 币种
 */
@Controller
@RequestMapping("/coin/token")
public class CoinTokenController extends BaseController {
    private String prefix = "coin/token";

    @Autowired
    private ICoinBaseSqlService baseSqlService;


    @GetMapping("/index")
    public String index() {
        return prefix + "/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Map<String, Object> parameterMap = MjkjUtils.getParameterMap(request);
        String orderByColumn = MjkjUtils.getMap2Str(parameterMap, "orderByColumn");
        String isAsc = MjkjUtils.getMap2Str(parameterMap, "isAsc");

        String symbol = MjkjUtils.getMap2Str(parameterMap, "symbol");
        String holders = MjkjUtils.getMap2Str(parameterMap, "holders");
        String beginTime = MjkjUtils.getMap2Str(parameterMap, "beginTime");
        String is_buy = MjkjUtils.getMap2Str(parameterMap, "is_buy");
        String is_sell = MjkjUtils.getMap2Str(parameterMap, "is_sell");
        Integer riskScore = MjkjUtils.getMap2Integer(parameterMap, "risk_score");
        if(riskScore<0){
            riskScore=null;
        }

        Page page = getPage();
        QueryWrapper wrapper = MjkjUtils.getQueryWrapper();
        wrapper.like(Func.isNotEmpty(symbol),"symbol",symbol);
        wrapper.gt(Func.isNotEmpty(holders),"holders",holders);
        wrapper.gt(Func.isNotEmpty(beginTime),"token_time",beginTime);
        wrapper.lt(Func.isNotEmpty(riskScore),"risk_score",riskScore);
        wrapper.eq(Func.isNotEmpty(is_buy),"is_buy",is_buy);
        wrapper.eq(Func.isNotEmpty(is_sell),"is_sell",is_sell);

        wrapper.select("*", "DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') create_time_str");
        if (Func.isNotEmpty(orderByColumn) && Func.isNotEmpty(isAsc)) {
            if (Func.equals(isAsc, "asc")) {
                wrapper.orderByAsc(orderByColumn);
            } else {
                wrapper.orderByDesc(orderByColumn);
            }
        } else {
            wrapper.orderByDesc("create_time");
        }

        IPage<Map<String, Object>> pages = baseSqlService.getDataIPageWrapper("coin_token", page, wrapper);

        return getDataTable(pages);
    }

}